#!/bin/bash

clear
echo "========================================================================="
echo "lamp v0.1 for VPS  Written by zitiger based on www.lnmp.org"
echo "========================================================================="
echo "A tool to auto-compile & install Apache+MySQL+PHP on CentOS "
echo "For more information please visit http://www.ztiger.com/"
echo ""
echo ""
echo "The path of some dirs:"
echo "mysql dir:   /usr/local/mysql"
echo "php dir:     /usr/local/php"
echo "apache dir:   /usr/local/apache"
echo "web dir      /home/www/default"
echo ""
echo "========================================================================="

if [ "$1" != "--help" ]; then
	get_char()
	{
	SAVEDSTTY=`stty -g`
	stty -echo
	stty cbreak
	dd if=/dev/tty bs=1 count=1 2> /dev/null
	stty -raw
	stty echo
	stty $SAVEDSTTY
	}



	domain="www.zitiger.com"
	echo "Please input domain:"
	read -p "(Default domain: www.zitiger.com):" domain

	echo ""
	mysqlpassword=""
	read -p "Please input mysql password:" mysqlpassword

	if [ "$domain" = "" ]; then
		domain="www.zitiger.com"
	fi

	echo ""
	groupadd www
	useradd -g www www
	echo ""
	echo "Please input ftp password:"
	passwd www

	echo ""
	echo "==========================="

	echo domain="$domain"

	echo "==========================="

	echo ""
	echo "Press any key to start installing..."
	char=`get_char`

yum -y upgrade
yum remove httpd -y
yum -y install yum-fastest-mirror
yum -y update
yum -y install wget make gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers vim-minimal patch tar unzip screen diffutils sendmail


echo "========================================================================="
echo "start download AMP package "

wget http://vps.googlecode.com/files/php-5.3.2.tar.gz
wget http://vps.googlecode.com/files/mysql-5.1.46.tar.gz
wget http://vps.googlecode.com/files/libiconv-1.13.1.tar.gz
wget http://vps.googlecode.com/files/libmcrypt-2.5.8.tar.gz
wget http://vps.googlecode.com/files/mcrypt-2.6.8.tar.gz
wget http://vps.googlecode.com/files/memcache-2.2.5.tgz
wget http://vps.googlecode.com/files/mhash-0.9.9.9.tar.gz
wget http://vps.googlecode.com/files/pcre-8.02.tar.gz
wget http://vps.googlecode.com/files/APC-3.1.3p1.tgz
wget http://vps.googlecode.com/files/httpd-2.2.15.tar.gz
wget http://vps.googlecode.com/files/cronolog-1.6.2.tar.gz
wget http://vps.googlecode.com/files/PDO_MYSQL-1.0.2.tgz
wget http://vps.googlecode.com/files/phpMyAdmin-3.3.2-all-languages.tar.gz



echo "download package finished!"
echo ""
echo "========================================================================="




tar zxvf libiconv-1.13.1.tar.gz
cd libiconv-1.13.1/
./configure --prefix=/usr/local
make
make install
cd ../

tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8/
./configure
make
make install
/sbin/ldconfig
cd libltdl/
./configure --enable-ltdl-install
make
make install
cd ../../

tar zxvf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9/
./configure
make
make install
cd ../

ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1

tar zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8/
./configure
make
make install
cd ../

echo "============================mysql install=================================="
tar -zxvf mysql-5.1.46.tar.gz
cd mysql-5.1.46
./configure --prefix=/usr/local/mysql --enable-assembler --with-extra-charsets=complex --enable-thread-safe-client --with-big-tables --with-readline --with-ssl --with-embedded-server --enable-local-infile
make;make install
cd ../
groupadd mysql
useradd -g mysql mysql
cp /usr/local/mysql/share/mysql/my-medium.cnf /etc/my.cnf
/usr/local/mysql/bin/mysql_install_db --user=mysql
chown -R mysql /usr/local/mysql/var
chgrp -R mysql /usr/local/mysql/.
cp /usr/local/mysql/share/mysql/mysql.server /etc/init.d/mysql
chmod 755 /etc/init.d/mysql
chkconfig --level 345 mysql on
echo "/usr/local/mysql/lib/mysql" >> /etc/ld.so.conf
echo "/usr/local/lib" >>/etc/ld.so.conf
ldconfig
ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql
ln -s /usr/local/mysql/include/mysql /usr/include/mysql
service mysql start
/usr/local/mysql/bin/mysqladmin -u root password $mysqlpassword
cat >>/etc/profile<<EOF
MYSQL_HOME=/usr/local/mysql 
PATH=$MYSQL_HOME/bin:$PATH 
export PATH
EOF
service mysql restart
service mysql stop
echo "============================mysql intall finished========================="

echo "============================apache httpd install======================"
tar zxvf httpd-2.2.15.tar.gz
cd httpd-2.2.15
./configure -enable-headers -enable-info -enable-rewrite -enable-so -prefix=/usr/local/apache
make
make install
cd ../
rm -rf /usr/local/apache/conf
cp -r apache_conf /usr/local/apache/conf
sed -i 's/www.zitiger.com/'$domain'/g' /usr/local/apache/conf/extra/httpd-vhosts.conf
echo "============================apache httpd install finished======================"

echo "============================php+apc install======================"
tar zxvf php-5.3.2.tar.gz
cd php-5.3.2/
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-apxs2=/usr/local/apache/bin/apxs --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-force-cgi-redirect --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-ftp --without-pear
make ZEND_EXTRA_LIBS='-liconv'
make install
mkdir -p /usr/local/php/etc
cp php.ini-dist /usr/local/php/etc/php.ini
cd ../

tar zxvf memcache-2.2.5.tgz
cd memcache-2.2.5/
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install
cd ../

tar zxvf APC-3.1.3p1.tgz
cd APC-3.1.3p1/
/usr/local/php/bin/phpize
./configure --enable-apc --enable-apc-mmap --with-php-config=/usr/local/php/bin/php-config
make
make install
cd ../


sed -i 's#extension_dir = "./"#extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"\nextension = "memcache.so"\nextension = "apc.so"\nextension = "pdo_mysql.so"\n#' /usr/local/php/etc/php.ini
sed -i 's#output_buffering = Off#output_buffering = On#' /usr/local/php/etc/php.ini

cat >>/usr/local/php/etc/php.ini<<EOF
apc.enabled = 1
apc.cache_by_default = on
apc.shm_segments = 1
apc.shm_size = 32
apc.ttl = 600
apc.user_ttl = 600
apc.num_files_hint = 0
apc.write_lock = On
EOF

mkdir -p /home/www/default
chmod +w /home/www/default
mkdir -p /home/logs
chmod 777 /home/logs

chown -R www:www /home/www/default
echo "============================php+eaccelerator install finished======================"

echo "============================pdo_mysql install======================"
tar -zxvf PDO_MYSQL-1.0.2.tgz
cd PDO_MYSQL-1.0.2
/usr/local/php/bin/phpize
./configure -with-php-config=/usr/local/php/bin/php-config -with-pdo-mysql=/usr/local/mysql
make
make install
cd ../
echo "============================pdo_mysql finished======================"

echo "============================cronolog install======================"
tar zxvf cronolog-1.6.2.tar.gz
cd cronolog-1.6.2
./configure
make
make install
cd ../
echo "============================cronolog install finished======================"

echo "============================set service======================"
echo "ulimit -SHn 51200" >>/etc/rc.local
echo "service mysql start" >>/etc/rc.local
echo "/usr/local/apache/bin/apachectl -k start" >>/etc/rc.local
/usr/local/apache/bin/apachectl -k start
service mysql start
echo "============================set service finished======================"


cat >/home/www/default/phpinfo.php<<eof
<?
phpinfo();
?>
eof
echo "============================phpMyAdmin install================================="
chown www -R  /home/
chown www /usr/local/apache/conf/
chown www /usr/local/php/etc/
yum -y install vsftpd
adduser www
/etc/init.d/vsftpd start
chkconfig --level 345 vsftpd on
cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
cp conf/index.php /home/www/default/index.php
cp conf/apc.php /home/www/default/apc.php

tar zxvf phpMyAdmin-3.3.2-all-languages.tar.gz
mv phpMyAdmin-3.3.2-all-languages /home/www/default/phpmyadmin
echo "============================phpMyAdmin install finished================================="

clear
echo "========================================================================="
echo "lamp v0.0.1 for VPS  Written by zitiger "
echo "========================================================================="
echo ""
echo "For more information please visit http://www.zitiger.com"
echo ""
echo "phpinfo test:http://$domain/phpinfo.php"
echo "phpMyAdmin test:http://$domain/phpmyadmin"
echo "The path of some dirs:"
echo "mysql dir:  /usr/local/mysql"
echo "php dir:  /usr/local/php"
echo "apache dir:  /usr/local/apache"
echo "web dir:  /home/www/default"
echo "ftp username:  www"
echo "mysql username:  root"
echo ""
echo "important:  login http://$domain/phpmyadmin and delete users with no password!"
echo ""
echo "========================================================================="
fi
